package com.bbx.taxi.client.Activity.Order;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
import butterknife.InjectView;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.UnprocessedList.Officail_Unprocessed;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.UnprocessedList.UnprocessedList;
import com.bbx.api.sdk.model.passanger.MyOrder;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.End;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.Location;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrderList;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrders;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.Start;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.official.passanger.Officail_UnprocessedNet;
import com.bbx.api.sdk.net.passeger.conn.MyOrderNet;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Activity.Order.OrderCancelReason;
import com.bbx.taxi.client.Activity.Order.OrderEvaluationActivity2;
import com.bbx.taxi.client.Activity.Order.OrderUnprocessed;
import com.bbx.taxi.client.Activity.Order.StartServiceActivity;
import com.bbx.taxi.client.Activity.Order.UnprocessedRecordActivity;
import com.bbx.taxi.client.Adapter.Unprocessed.UnprocessedAdapter;
import com.bbx.taxi.client.Adapter.Unprocessed.UnprocessedRecordAdapter;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Bean.Extra.ExamineMsg;
import com.bbx.taxi.client.Bean.Extra.OrderMsg;
import com.bbx.taxi.client.DB.MyOrder.MyOrderDB;
import com.bbx.taxi.client.DB.UnprocessedList.UnprocessedListDB;
import com.bbx.taxi.client.DB.UnprocessedList.UnprocessedRecordDB;
import com.bbx.taxi.client.DB.Util.DBcolumns;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.Task.MyUnprocessedTask;
import com.bbx.taxi.client.Task.MyUnprocessedTask.OnUnprocessedFinishListener;
import com.bbx.taxi.client.Util.ClickFilter;
import com.bbx.taxi.official.R;
import com.example.loadview.LoadingDialog;
import com.example.mylistview.pullListView.PullToRefreshLayout;
import com.example.mylistview.pullListView.PullableListView;
import com.example.mylistview.pullListView.PullToRefreshLayout.OnRefreshListener;
import com.example.mylistview.pullListView.PullableListView.OnLoadListener;
import com.example.mylistview.swipemenu.MyListView;
import com.example.mylistview.swipemenu.MyListView.OnSwipeListener;
/** 
 * @Path com.bbx.taxi.client.Activity.Order.UnprocessedRecordActivity.java
 * @Author 严旭文
 * @Date 2016-1-26 下午5:23:08
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 * 审核记录
 */
public class UnprocessedRecordActivity extends BaseBBXActivity implements OnUnprocessedFinishListener{
	@InjectView(R.id.layout_nodata)LinearLayout layout_nodata;
	@InjectView(R.id.MyListView)
	MyListView mMyListView;
	@InjectView(R.id.pullToRefreshLayout)
	PullToRefreshLayout pullToRefreshLayout;
	private UnprocessedRecordDB db_unprocessedrecord;// 审核记录据库
	public UnprocessedRecordAdapter mUnprocessedRecordAdapter;
//	private UnprocessedTask mUnprocessedTask;
	/** 加载跟刷新的数量 */
	public static int num = 10;
	private int start_id = 0;
	private boolean isFirst = true;
	private ArrayList<UnprocessedList> list_processed = new ArrayList<UnprocessedList>();

	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_menu_examine);
		super.onCreate(savedInstanceState);
	
	}
	@Override
	protected void initView() {
		setTitle(getString(R.string.title_unprocessed_record));
		mUnprocessedRecordAdapter = new UnprocessedRecordAdapter(context, list_processed);
		mMyListView.setAdapter(mUnprocessedRecordAdapter);

	}

	@Override
	protected void initData() {
		db_unprocessedrecord = new UnprocessedRecordDB(this);
		initMyListView();
		onReadDB(true);
	}
	private void initMyListView() {
		pullToRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				refreshAllOrder(true);
			}
		});
		
		mMyListView.setOnLoadListener(new OnLoadListener() {

			@Override
			public void onLoad(PullableListView pullableListView) {
				loadMoreAllOrder();
			}
		});
		mMyListView.setEmptyView(layout_nodata);
		mMyListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(!ClickFilter.filter(1000L))return;
				// TODO【 修正有Header的ListView的position的BUG 】
				int headerViewsCount = mMyListView
						.getHeaderViewsCount();// 得到header的总数量
				// 【得到新的修正后的position】
				int newPosition = position - headerViewsCount;
				OrderUnprocessed.setOrder(list_processed.get(newPosition));
				context.startActivity(new Intent(context,OrderUnprocessed.class)
				.putExtra(ExamineMsg.EXAMINE_TYPE,ExamineMsg.TYPE_UN)
				.putExtra(ExamineMsg.EXAMINE_STATUS,ExamineMsg.STATUS_RECORD)); 
 
			}
		});
		mMyListView.setOnSwipeListener(new OnSwipeListener() {

			@Override
			public void onSwipeStart(int position) {
				pullToRefreshLayout.setIsProhibitPull(true);
			}

			@Override
			public void onSwipeEnd(int position) {
				pullToRefreshLayout.setIsProhibitPull(false);
			}
		});

	}
	/**
	 * 读取全部订单数据库数据,根据类型，isLoad=true则出现加载框
	 */
	private void onReadDB(final boolean isLoad) {
		new AsyncTask<Void, Void, Void>() {
			protected void onPreExecute() {
				if (loaddialog != null && !loaddialog.isShowing() && isLoad) {
					loaddialog.show();
				}
				super.onPreExecute();
			}

			@Override
			protected Void doInBackground(Void... params) {
				list_processed.clear();
				list_processed.addAll(db_unprocessedrecord.onReadDB(context, num));
				return null;
			}

			protected void onPostExecute(Void result) {
				notifyDataSetChanged(mMyListView, mUnprocessedRecordAdapter, list_processed.size(),
						num);
				if (isFirst) {
					isFirst = false;
					if (loaddialog != null && !loaddialog.isShowing()) {
						loaddialog.show();
					}
					refreshAllOrder(false);
				} else {
					if (loaddialog != null) {
						loaddialog.onDismiss();
					}
				}

				if (list_processed.size() >= num) {
					//start_id暂时隐藏
//					start_id = list.get(list.size() - 1).getId();
					mMyListView.setHasMoreData(true);
				}
			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	/**
	 * 刷新全部的订单，isPull判断是否下啦刷新还是，自动刷新
	 */
	public void refreshAllOrder(boolean isPull) {
		start_id=0;
		if(!isPull){
			if(loaddialog!=null&&!loaddialog.isShowing()){
				loaddialog.show();
			}
		}
		MyUnprocessedTask.getInstance(context, num, start_id,MyUnprocessedTask.STATUS_PROCESSED).setOnOrderFinishListener(this);

	}
	private void loadMoreAllOrder() {
		start_id=start_id+num;
		MyUnprocessedTask.getInstance(context, num, start_id,MyUnprocessedTask.STATUS_PROCESSED).setOnOrderFinishListener(this);
	};
	public void notifyDataSetChanged(final MyListView mMyListView,
			final BaseAdapter adapter, int size, int num) {
		if (size >= num) {
			mMyListView.setHasMoreData(true);
		} else {
			mMyListView.setHasMoreData(false);
		}
		adapter.notifyDataSetChanged();
	}

	/**
	 * 只用于删除
	 */
	public void notifyDataSetChanged(final MyListView mMyListView,
			final BaseAdapter adapter) {
		adapter.notifyDataSetChanged();

	}
    @Override
    public void onClick(View v) {
         switch(v.getId()){
         case R.id.tv_right:
         startActivity(new Intent(this,UnprocessedRecordActivity.class));
          break;
         default:
        	 super.onClick(v);
        	 break;
         }
   
    }
	/**
	 * 写入全部订单数据
	 */
	private void onWriteDB(ArrayList<UnprocessedList> list, boolean isDelete) {
		isWriteFinish = false;
		db_unprocessedrecord.onWriteDB(this, list, isDelete, mHandler);
	}
	/**
	 * 删除全部所有数据
	 */
	public void onDeleteDB() {
		db_unprocessedrecord.onDelete();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	@Override
	public void onUnprocessedFinish(int status,
			ArrayList<UnprocessedList> list, int allcount) {
		if (loaddialog != null) {
			loaddialog.dismiss();
		}
		switch(status){
		  case MyUnprocessedTask.STATUS_SUCCESS:
			  if(start_id==0){//刷新
				  list_processed.clear();
				  if (list != null) {
					  list_processed.addAll(list);
						pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						notifyDataSetChanged(mMyListView, mUnprocessedRecordAdapter,
								list_processed.size(), num);
						onWriteDB(list_processed, true);
					}
			  }else{//加载
				  if (list != null&&!list.isEmpty()) {
					  list_processed.addAll(list);
						onWriteDB(list_processed, true);
						notifyDataSetChanged(mMyListView, mUnprocessedRecordAdapter,
								list.size(), num);
				  }else{
					  mMyListView.setHasMoreData(false);
						ToastUtil.showToast(context,
								getString(R.string.in_the_end));
				  }
			  }
			  break;
		  case MyUnprocessedTask.STATUS_FAil:
			  ToastUtil.showToast(context,
						getString(R.string.no_network));
			  
			  break;
		  case MyUnprocessedTask.STATUS_NULL:
			  if(start_id==0){//刷新
				  list_processed.clear();
				  pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
				  notifyDataSetChanged(mMyListView, mUnprocessedRecordAdapter,
							0, num);
					onWriteDB(list_processed, true);
			  }else{
				  mMyListView.setHasMoreData(false);
					ToastUtil.showToast(context,
							getString(R.string.in_the_end));
			  }

			  break;
		}
	}
}
